import React, { useEffect, useState } from "react";
import '../../appcommon.css';
import styles from './earn.module.css';
import { useDispatch } from "react-redux";
import { DotLoading, Mask } from "antd-mobile";
import { saveEvent } from "@/store/actions/saveEvent";
import { useHistory } from "react-router-dom/cjs/react-router-dom.min";
import icon_nav_back_white from "@/assets/icon_nav_back_white.png"


import NavigateBar from "@/components/NavigateBar";
import EmptyView from "@/components/EmptyView/emptyView";
import { LangThai } from "@/utils/langThai";
import BottomTabBar from "@/components/BottomTabBar/bottomTabBar";
import { getBills } from "@/store/actions/user";

/**
 * 登录页
 */
export default function Earn() {

    //loading状态
    const [maskVisible, setMaskVisible] = useState(false)

    //显示add home弹框
    const [addHomeMaskVisible, setAddHomeMaskVisible] = useState(false);
    const [addHomeMaskIsHomeMode, setAddHomeMaskIsHomeMode] = useState(true);

    // 当前 列表
    const [curList, setCurList] = useState([]);

    // 文案翻译
    const [curLang, setCurLang] = useState(LangThai);

    // 获取 Redux 分发器
    const dispatch = useDispatch()

    //router
    const history = useHistory()

    //清空登录信息
    useEffect(() => {
        //关闭loading
        setMaskVisible(false);

        // 查询账单
        doGetBills();

        //打点
        doHitEvent("INVITE_DETAIL_START")

        return () => {
            //打点
            doHitEvent("INVITE_DETAIL_END")
        }
    }, [])

    // 获取账单 返回
    const onDoGetBillsCallback = (success, data) => {
        //关闭loading
        setMaskVisible(false)
        console.log('获取账单 返回: ' + success);

        if (success && data) {
            // 处理流程业务
            if (data.length > 0) {
                // [
                //     {
                //         "createBy": null,
                //         "createTime": "2024-01-16 13:42:00",
                //         "updateBy": null,
                //         "updateTime": "2024-01-16 13:42:00",
                //         "remark": null,
                //         "id": 3,
                //         "userId": "1196600121492647937",
                //         "amount": "100",
                //         "type": "购物",
                //         "detail": "测试"
                //     }
                // ]

                // 保存账单
                setCurList(data);
            }
        }
    };

    //打点接口
    const doHitEvent = async (type, remark = "") => {
        console.log("doHitEvent: " + type + " , " + remark);

        try {
            //获取Action
            const action = saveEvent(type, remark)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    // 获取账单
    const doGetBills = async () => {
        console.log("doGetBills: ");

        try {
            //显示loading
            setMaskVisible(true)

            //获取Action
            const action = getBills(onDoGetBillsCallback)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }


    return (
        <div className='pageRoot pageBackgroundColorGray'>
            <NavigateBar theme="white" leftIcon={icon_nav_back_white} onAddFavorite={() => { setAddHomeMaskIsHomeMode(false); setAddHomeMaskVisible(true) }} onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }}>明细</NavigateBar>

            {/* 使用添加 home screen bar */}
            {/* <AddFavorite onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }} /> */}

            <div className="pageContentColumnFull">
                {/* 菜单 列表 */}
                <div className="pageContentColumnFull">
                    <div className={styles.inviteDetailList}>
                        {curList && curList.map((value, index) => {
                            return (
                                <div className={styles.inviteDetailListItem} key={index}>
                                    <div className={styles.inviteDetailListItemRow}>
                                        <div className={`${styles.inviteDetailListItemTextRow} pageMarginTop8px`}>
                                            <div className={styles.inviteDetailListItemTag}>
                                                <div className={styles.inviteDetailListItemTagValue}>{value.tag}</div>
                                            </div>
                                            <div className={styles.inviteDetailListItemDetail}>{value.createTime}</div>
                                        </div>

                                        <div className={styles.inviteDetailListItemTextRow}>
                                            <div className={styles.inviteDetailListItemTitle}>金额</div>
                                            {value.type === "收入" && <div className={styles.inviteDetailListItemDetailBold}>+{value.amount}</div>}
                                            {value.type !== "收入" && <div className={`${styles.inviteDetailListItemDetailBold} pageTextColorRed`}>-{value.amount}</div>}
                                        </div>

                                        <div className={styles.inviteDetailListItemTextRow}>
                                            <div className={styles.inviteDetailListItemTitle}>类型</div>
                                            <div className={styles.inviteDetailListItemDetail}>{value.type}</div>
                                        </div>

                                        <div className={styles.inviteDetailListItemTextRow}>
                                            <div className={styles.inviteDetailListItemTitle}>备注</div>
                                            <div className={styles.inviteDetailListItemDetail}>{value.detail}</div>
                                        </div>
                                    </div>
                                </div>
                            )
                        })}
                    </div>

                    {(!curList || (curList.length === 0)) && <EmptyView></EmptyView>}

                </div>
            </div>

            {/* 底部 margin */}
            <div className="pageBottomEdge"></div>

            {/* tab bar */}
            <BottomTabBar page="earn" />


            {/* load mask */}
            <Mask visible={maskVisible} opacity={0.1}
                onMaskClick={() => { }}>
                <div className="pageLoadingOverlayContent">
                    <span style={{ fontSize: 24, color: '#3498DB' }} >
                        <DotLoading color="currentColor" />
                    </span>
                </div>
            </Mask>
        </div >
    )
}